<?php

namespace App\Http\Controllers;

use App\Finish;
use App\Task;
use App\Work;
use App\Worker;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Rule;

class PayrollController extends Controller
{
    public function __construct() {
        $this->middleware('auth');
    }
    public function index()
    {
        $uid=\Auth::id();
        $rules=Rule::where('uid',$uid)->get();
        return view('info.home',['rules'=>$rules]);
    }



    public function showmore(Request $request)
    {

        $id=$request->input('workerId');
        $date=$request->input('date');
        $date= Carbon::createFromFormat('Y-m-d H', $date.'-01 00');
        $firstDay=$date->firstOfMonth();
        $firstDay=date('Y-m-d',strtotime($firstDay) );
        $endDay=$date->lastOfMonth();
        $endDay=date('Y-m-d',strtotime($endDay) );
        $tasks= Task::where('workerId',$id)->whereBetween('date',[$firstDay,$endDay])->get();
        return response()->json($tasks);
    }
    public function getall(Request $request)
    {
        $date=$request->input('date');
        $date= Carbon::createFromFormat('Y-m-d H', $date.'-01 00');
        $firstDay=$date->firstOfMonth();
        $firstDay=date('Y-m-d',strtotime($firstDay) );
        $endDay=$date->lastOfMonth();
        $endDay=date('Y-m-d',strtotime($endDay) );
        if($request->input('workshopId') !='' ){
            $workers=Worker::where('workshopId',$request->input('workshopId'))->get();
                foreach($workers as $wk) {
                  $newworkers[]=$wk;
                }
            }else{
            $workers=Worker::all();
                foreach($workers as $wk) {
                    $newworker=$wk;
                    $newworker['output']=Task::where('workerId',$wk->id)->whereBetween('date',[$firstDay,$endDay])->sum('output');
                    $newworker['finish']=Task::where('workerId',$wk->id)->whereBetween('date',[$firstDay,$endDay])->sum('finish');

                    $newworker['info']=$newworker['finish']*$wk->workerWage;
                    if($wk->workerWageType == '按月'){
                        $newworker['info']=$wk->workerWage;
                    }
                    $newworkers[]=$newworker;
                }
               return response()->json($newworkers);
            }

    }
}
